﻿@using System.Collections.ObjectModel
<CascadingValue Name="RightToLeft" Value="@RightToLeft" IsFixed="true">
    <MudDataGrid @ref="_dataGrid" Items="@_items" Filterable="true" ExpandSingleRow="@ExpandSingleRow">
        <Columns>
            <HierarchyColumn EnableHeaderToggle="@EnableHeaderToggle" ButtonDisabledFunc="@(x => x.Name == "Alicia")" InitiallyExpandedFunc="@(x => x.Name == "Ira" || x.Name == "Anders")" />
            <PropertyColumn Property="x => x.Name" />
            <PropertyColumn Property="x => x.Age" />
            <PropertyColumn Property="x => x.Status" />
        </Columns>
        <ChildRowContent>
            <tr>
                <td colspan="3">@($"uid = {context.Item.Name}|{context.Item.Age}|{context.Item.Status}|{Guid.NewGuid()}")</td>
            </tr>
        </ChildRowContent>
    </MudDataGrid>
</CascadingValue>
<MudButton OnClick="@ExpandAll" Color="@Color.Primary">Expand All</MudButton>
<MudButton OnClick="@CollapseAll" Color="@Color.Primary">Collapse All</MudButton>
<MudSwitch @bind-Value="EnableHeaderToggle" Label="Enable Header Toggle" />
<MudSwitch @bind-Value="ExpandSingleRow" Label="Expand Single Row" />
@code {
    public static string __description__ = "A test for Initially Expanded when Items is an observable collection and added after the fact.";
    [Parameter]
    public bool RightToLeft { get; set; }

    [Parameter]
    public bool EnableHeaderToggle { get; set; }

    [Parameter]
    public bool ExpandSingleRow { get; set; }

    private MudDataGrid<Model> _dataGrid = null!;

    private readonly ObservableCollection<Model> _items = [];

    private readonly IReadOnlyList<Model> _itemList = 
        new List<Model>
        {
            new Model("Sam", 56, Severity.Normal),
            new Model("Alicia", 54, Severity.Info),
            new Model("Ira", 27, Severity.Success),
            new Model("John", 32, Severity.Warning),
            new Model("Anders", 24, Severity.Error)
        };

    protected override void OnAfterRender(bool firstRender)
    {
        base.OnAfterRender(firstRender);
        if (firstRender)
        {
            foreach (var model in _itemList)
            {
                _items.Add(model);
            }

            StateHasChanged();
        }
    }

    private Task ExpandAll()
    {
        return _dataGrid.ExpandAllHierarchy();
    }

    private Task CollapseAll()
    {
        return _dataGrid.CollapseAllHierarchy();
    }

    public record Model(string Name, int Age, Severity Status);
}
